package com.yl.ydk.ui.fragment

import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import androidx.annotation.RequiresApi
import com.yl.ydk.R
import com.yl.ydk.base.BaseApplication
import com.yl.ydk.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_dissease_detail_item.*
import java.io.InputStream
import java.net.URL

class DiseaseDetailItemFragment : BaseFragment() {

    private var text: String? = null

    companion object {
        fun getInstance(text: String): DiseaseDetailItemFragment {
            val sf = DiseaseDetailItemFragment()
            sf.text = text
            return sf
        }
    }

    override fun getLayoutResId(): Int = R.layout.fragment_dissease_detail_item

    override fun attachView() {

    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        BaseApplication.MAIN_EXECUTOR.submit {
            val sp = Html.fromHtml(text, object : Html.ImageGetter {
                override fun getDrawable(source: String?): Drawable? {
                    var inputStream: InputStream? = null
                    try {
                        inputStream = URL(source).content as InputStream
                        val d = Drawable.createFromStream(inputStream, "src")
                        d.setBounds(0, 0, d.intrinsicWidth, d.intrinsicHeight)
                        inputStream.close()
                        return d
                    } catch (e: Exception) {
                        return null
                    }
                }
            }, null)
            activity?.let {
                it.runOnUiThread {
                    tvName.text = sp
                }
            }
        }
    }

}